<?php
/**
 * Created by IntelliJ IDEA.
 * User: shihuipeng
 * Date: 2017/6/7
 * Time: 上午10:11
 */

namespace app\helpers;


class Utils
{
    public static function wrap_message_success(array $ext = null, $json_encode = true, $json_options = null)
    {
        return self::wrap_message('0', 'ok', $ext, $json_encode, $json_options);
    }

    public static function wrap_message($ret_code, $ret_msg = '', array $ext = null, $json_encode = true, $json_options = null)
    {
        $ret_code = "$ret_code";

        if (is_array($ext))
            $ret = array_merge(compact('ret_code', 'ret_msg'), $ext);
        else
            $ret = compact('ret_code', 'ret_msg');

        if ($json_options != null)
            $json_options = JSON_UNESCAPED_UNICODE | $json_options;
        else
            $json_options = JSON_UNESCAPED_UNICODE;

        if ($json_encode) $ret = json_encode($ret, $json_options);

        return $ret;
    }

    public static function save_file($file_path,$file)
    {
        $fp2 = @fopen($file_path, 'w');
        fwrite($fp2, $file);
        $res = fclose($fp2);
        return $res;
    }

    public static function curl($url,$header,$data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    public static function date_time($time = '')
    {
        if(is_numeric($time)){
            return date('Y-m-d H:i:s',$time);
        }
        return date('Y-m-d H:i:s',time());
    }

    public static function var_export_inline($obj)
    {
        return str_replace("\n", '', var_export($obj, true));
    }
}

